package com.choudou5.tools.excel;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSON;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

public class BasicExcelHelper {

    /**
     * 读取数据
     * @param filePath
     * @param sheetIndex tab下标 0开始
     * @return
     * @throws Exception
     */
    protected static List<List<Object>> readData(String filePath, int sheetIndex) throws Exception {
        File file = new File(filePath);
        ExcelReader reader = ExcelUtil.getReader(new FileInputStream(file), sheetIndex);
        List<List<Object>> list = reader.read(0);
        return list;
    }

    protected static <T> List<T> readData(String filePath, int sheetIndex, Class<T> beanType) throws Exception {
        File file = new File(filePath);
        ExcelReader reader = ExcelUtil.getReader(new FileInputStream(file), sheetIndex);
        List<T> list = reader.read(0, 1, beanType);
        return list;
    }

    protected static void printData(List list){
        if(CollUtil.isEmpty(list)){
            System.out.println("打印为空");
            return;
        }
        list.forEach(row -> {
            System.out.println(JSON.toJSONString(row));
        });
    }

}
